package com.csu.tour.controller;

import com.csu.tour.common.CommonResponse;
import com.csu.tour.entity.Emergency;
import com.csu.tour.service.EmergencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emergency")
public class EmergencyController {
    @Autowired
    private EmergencyService emergencyService;

    @GetMapping
    public CommonResponse<List<Emergency>> getPublishedEmergency() {
        List<Emergency> emergencyList = emergencyService.selectEmergencyByStatus("published");
        boolean temp = emergencyList.isEmpty();
        //数据库查询成功但数据库中没有任何数据
        if (temp) {
            String msg="当前没有应急信息";
            return CommonResponse.createForSuccessMessage(msg);
        }else {
            // 创建成功响应，包含数据
            String msg="数据库查询成功";
            return CommonResponse.createForSuccess(emergencyList);
        }
    }

    @GetMapping("/select")
    public CommonResponse<List<Emergency>> selectEmergencyByContent(@RequestBody Emergency emergency) {
        List<Emergency> emergencyList = emergencyService.selectEmergencyByContent(emergency.getContent());
        boolean temp = emergencyList.isEmpty();
        //数据库查询成功但数据库中没有任何数据
        if (temp) {
            String msg="当前没有应急信息";
            return CommonResponse.createForSuccessMessage(msg);
        }else {
            // 创建成功响应，包含数据
            String msg="数据库查询成功";
            return CommonResponse.createForSuccess(emergencyList);
        }
    }
    @GetMapping("/get")
    public CommonResponse<List<Emergency>> getAllEmergency() {
        List<Emergency> emergencyList = emergencyService.selectAllEmergency();
        boolean temp = emergencyList.isEmpty();
        //数据库查询成功但数据库中没有任何数据
        if (temp) {
            String msg="当前没有应急信息";
            return CommonResponse.createForSuccessMessage(msg);
        }else {
            // 创建成功响应，包含数据
            String msg="数据库查询成功";
            return CommonResponse.createForSuccess(emergencyList);
        }
    }



    @RequestMapping("/commit")
    public CommonResponse<Emergency> insertEmergency(@RequestBody Emergency emergency) {
        if(emergencyService.insertEmergency(emergency)==1)
        {
            String msg="提交成功";
            return CommonResponse.createForSuccess(msg,emergency);
        }else{
            return CommonResponse.createForError(500,"应急信息提交失败");
        }
    }

    @DeleteMapping( "/delete")
    public CommonResponse<Emergency> deleteEmergency(@RequestBody Emergency emergency) {
        if (emergencyService.deleteEmergency(emergency.getId()) == 1) {
            String msg = "删除成功";
            return CommonResponse.createForSuccess(msg, null);
        } else {
            return CommonResponse.createForError(500, "应急信息删除失败");
        }
    }

    @PutMapping("/update")
     public CommonResponse<Emergency> updateEmergency(@RequestBody Emergency emergency) {
        if (emergencyService.updateEmergency(emergency) == 1) {
            String msg = "更新成功";
            return CommonResponse.createForSuccess(msg, emergency);
        } else {
            return CommonResponse.createForError(500, "应急信息更新失败");
        }
    }
}
